/**
 * 
 */
package top.lcpsky.flashcards.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Joiner;

import top.lcpsky.flashcards.common.factory.PageFactory;
import top.lcpsky.flashcards.common.page.Page;
import top.lcpsky.flashcards.common.page.PageInfoBT;
import top.lcpsky.flashcards.dto.StockSimpleInfo;
import top.lcpsky.flashcards.entity.Stock;
import top.lcpsky.flashcards.service.SinaSharesRepository;
import top.lcpsky.flashcards.service.StockService;
import top.lcpsky.flashcards.util.Pager;

/**
 * @ClassName: StockController
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: lcp
 * @date: 2020年7月13日 上午9:17:48
 * 
 * @Copyright: 2020 www.ankept.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("stock")
public class StockController {

	@Autowired
	private SinaSharesRepository repos;
	@Autowired
	private StockService stockSvc;

	@RequestMapping(value = "/rtinfo")
	@ResponseBody
	public Object list() {
		String code = "";
		List<Stock> stocks = stockSvc.getStockChecked();
		if (CollectionUtils.isEmpty(stocks)) {
			code = "600118";
		} else {
			code = Joiner.on(",").join(stocks.stream().map(e->e.getCode()).collect(Collectors.toList()));
		}
		Map map = new HashMap();
		map.put("code", code);
		Page<StockSimpleInfo> page = new PageFactory<StockSimpleInfo>().defaultPage();
		Pager<StockSimpleInfo> pages = repos.getPage(map, page.getOffset(), page.getLimit());
		page.setTotal(pages.getTotal());
		page.setRecords(pages.getList());
		return new PageInfoBT<>(page);
	}

	@RequestMapping(value = "/check")
	@ResponseBody
	public String add(String content) {
		return stockSvc.enableStock(content).toString();
	}
}
